package com.compass.mobile.ui.fragment

import LogUtil
import UserInfoManager
import android.content.pm.PackageManager.NameNotFoundException
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import com.compass.mobile.databinding.FragmentInquiryBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.constant.INQUIRY
import com.compass.mobile.framework.constant.INQUIRY_ACTION
import com.compass.mobile.framework.constant.YD_USER_ID
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.framework.utils.LiveDataBus
import com.compass.mobile.models.Attach
import com.compass.mobile.models.Config
import com.compass.mobile.models.DoctorInfo
import com.compass.mobile.ui.dialog.CodeDialog
import com.compass.mobile.ui.dialog.OrderCodeDialog
import com.compass.mobile.ui.viewModel.DoctorViewModel
import toJson

/**
 * Description： 医生主页----问诊界面子页面
 * Date:2024/5/23 14:02
 */
class InquiryFragment : BaseVBFragment<FragmentInquiryBinding>() {

    private val doctorViewModel: DoctorViewModel by activityViewModels()
    override fun initView(view: View, savedInstanceState: Bundle?) {

        FocusViewUtils.scaleView(mBinding?.llFollow, 0)
        FocusViewUtils.scaleView(mBinding?.llConsultationRecord, 0)
        FocusViewUtils.scaleView(mBinding?.llImageText, 0)
        FocusViewUtils.scaleView(mBinding?.llPhone, 0)
        FocusViewUtils.scaleView(mBinding?.llVideo, 0)

    }

    private var doctorId: String? = null
    private var deptId: String? = null
    private var txtPrice: String? = null
    private var phonePrice: String? = null
    private var videoPrice: String? = null
    override fun initData() {
        doctorViewModel.doctorHomePageLiveData.observe(this) {
            it?.let {
                initDoctorInfo(it.doctor, it.config)
            }
        }
        doctorViewModel.followLiveData.observe(this) {
            it?.let {
                setFollow(it)
            }
            dismissLoading()
        }

        LiveDataBus.get().with(INQUIRY_ACTION, Attach::class.java)
            .observe(this) {
                LogUtil.d("attach===${it.toJson()}")
                //{"id":0,"orderId":1810284600665161729,"type":"2"}
                if (::oderCodeDialog.isInitialized) {
                    oderCodeDialog.dismissDialog()
                }
                var orderId = if (it.id.isNullOrEmpty()) {
                    it.orderId
                } else {
                    it.id
                }
                OrderCodeDialog.Builder(requireActivity(), orderId, it.type, INQUIRY_ACTION).show()

            }

    }


    /**
     * 设置医生信息
     */

    private fun initDoctorInfo(doctorHomePage: DoctorInfo, config: Config) {
        doctorId = doctorHomePage.id
        deptId = doctorHomePage.departmentId
        txtPrice = config.txtPrice
        phonePrice = config.phonePrice
        videoPrice = config.videoPrice
        setFollow(doctorHomePage.follow)
        mBinding?.tvTxtDate?.text = "${config.txtDate}${config.txtTime}"
        mBinding?.tvImageTextPrice?.text = "￥${config.txtPrice}元/次"
        mBinding?.tvPhoneDate?.text = "${config.phoneDate} ${config.phoneTime}"
        mBinding?.tvPhonePrice?.text = "￥${config.phonePrice}元/次"
        mBinding?.tvVideoPrice?.text = "￥${config.videoPrice}元/次"
        mBinding?.tvVideoDate?.text = "${config.videoDate} ${config.videoTime}"


    }

    private fun setFollow(follow: String) {
        mBinding?.tvFollow?.text = if (follow == "1") "已关注" else "关注医生"
    }

    private lateinit var oderCodeDialog: CodeDialog.Builder
    override fun intListener() {
        mBinding?.llFollow?.onClick {
            showLoading()
            doctorViewModel.follow(doctorId, UserInfoManager.getPatientInfo()?.id)

        }


        mBinding?.llImageText?.onClick {
            var string = "type={1图文 2电话 3视频}&doctorId=医生id&deptId=科室id&price=金额"//样例
            var inquiryInfo =
                "${UserInfoManager.getString(YD_USER_ID)}&type=1&doctorId=$doctorId&deptId=$deptId&price=$txtPrice"
            oderCodeDialog = CodeDialog.Builder(requireActivity(), INQUIRY + inquiryInfo)
            oderCodeDialog.show()
        }

        mBinding?.llPhone?.onClick {
            var inquiryInfo =
                "${UserInfoManager.getString(YD_USER_ID)}&type=2&doctorId=$doctorId&deptId=$deptId&price=$phonePrice"
            oderCodeDialog = CodeDialog.Builder(requireActivity(), INQUIRY + inquiryInfo)
            oderCodeDialog.show()
        }

        mBinding?.llVideo?.onClick {
            var inquiryInfo =
                "${UserInfoManager.getString(YD_USER_ID)}&type=3&doctorId=$doctorId&deptId=$deptId&price=$videoPrice"
            oderCodeDialog = CodeDialog.Builder(requireActivity(), INQUIRY + inquiryInfo)
            oderCodeDialog.show()


//            val isBrowserInstalled = isAppInstalled("com.starcor.browser.app.jkyl");
//
//            if (isBrowserInstalled) {
//                val intent=Intent(Intent.ACTION_VIEW, Uri.parse("native://agedcare.large/order-select"))
//                startActivity(intent)
//            } else {
//                showToast("请先安装爱家健康")
//
//            }

        }
    }




    private fun isAppInstalled(packageName: String):Boolean{
        try {
            requireActivity().packageManager.getPackageInfo(packageName,0)
            return true
        } catch (e:NameNotFoundException){
            return false
        }
    }
}